﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "CmdCreateStructureCustomizedProfileBeam.h"
#include "GmUiStructureCommandRegister.h"
#include "StructureCmdIds.h"
#include "StructureDefaultFamilyNames.h"
#include "StructureBeamInput.h"
#include "StructureInstanceCmdUtils.h"
#include "StructureLinearInstanceCreator.h"
#include "GbmpBuiltInCategoryUniIdentitiesNew.h"
#include "AddStructureDynamicPannelUtil.h"
#include "ActionBase.h"
#include "ActionDrawStraightElementInput.h"
#include "StructureInstanceSecondKeys.h"
#include "IModelView.h"
#include "GbmpModelViewTypes.h"
#include "UiCommonDialog.h"
#include "UiDocumentViewUtils.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;

REGISTER_GM_COMMAND(CmdCreateStructureCustomizedProfileBeam)

CmdCreateStructureCustomizedProfileBeam::CmdCreateStructureCustomizedProfileBeam()
    : CmdCreateStraightStructureInstanceBase(ID_CMD_CREATE_STRAIGHT_STRUCTURE_CUSTOMIZED_PROFILE_BEAM)
{
}

CmdCreateStructureCustomizedProfileBeam::~CmdCreateStructureCustomizedProfileBeam()
{
}

StructureFamilyUiUtils::FamilyCreators CmdCreateStructureCustomizedProfileBeam::GetDefaultFamilyCreators()
{
    StructureFamilyUiUtils::FamilyCreators defaultFamilyCreators{}; // 为空
    return defaultFamilyCreators;
}

std::wstring CmdCreateStructureCustomizedProfileBeam::GetDefaultFamilyName()
{
    return DEFAULT_CUSTOMIZED_SECTION_BEAM_FAMILY_NAME;
}

OwnerPtr<StructureLinearInstanceInput> CmdCreateStructureCustomizedProfileBeam::GetInstanceInput(const std::vector<Vector3d>& pts) const
{
    IDocument* pDoc = UiDocumentViewUtils::GetCurrentDocument();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDoc, L"pDoc为空",L"GDMPLab",L"2024-03-30");

    IModelView* pModelView = UiDocumentViewUtils::GetCurrentModelView();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pModelView, L"pModelView为空",L"GDMPLab",L"2024-03-30");

    OwnerPtr<StructureBeamInput> beamInput = NEW_AS_OWNER_PTR(StructureBeamInput);

    // 定位点直接设置为绘制线的基点
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(!pts.empty(), L"PositioningPoints为空",L"GDMPLab",L"2024-03-30");
    beamInput->PositionPoints = pts;

    bool bOk = StructureInstanceCmdUtils::GenerateStraightBasicBeamInput(pDoc, pModelView,
        this->GetCategory(), this->GetCategorySecondKey(), m_familyId, m_instanceTypeId, *beamInput);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(bOk, L"GenerateStraightBasicBeamInput失败",L"GDMPLab",L"2024-03-30");

    return TransferOwnership(beamInput);
}

OwnerPtr<IStructureLinearInstanceCreator> CmdCreateStructureCustomizedProfileBeam::GetInstanceCreator() const
{
    return NEW_AS_OWNER_PTR(StructureStraightBasicBeamCreator);
}

UniIdentity CmdCreateStructureCustomizedProfileBeam::GetCategory() const
{
    return BuiltInCategoryUniIdentities::BICU_STRUCTURE_BEAM;
}

void gcmp::CmdCreateStructureCustomizedProfileBeam::SetupRibbonContextualPanel()
{
    AddStructureDynamicPannelUtil::SetupCustomizedProfileBeamCreationContext(m_pAction, true, true);
}

Int32 CmdCreateStructureCustomizedProfileBeam::GetCategorySecondKey() const
{
    return (Int32)StructureBeamType::CustomizedProfile;
}

bool CmdCreateStructureCustomizedProfileBeam::OnExecuteCommand() const
{
    IModelView* pModelView = UiDocumentViewUtils::GetCurrentModelView();
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pModelView, L"pModelView为空",L"GDMPLab",L"2024-03-30");
    const std::wstring& viewType = pModelView->GetUserDataId();

    if (PlanViewType != viewType && View3DViewType != viewType)
    {
        UiCommonDialog::ShowMessageBox(GBMP_TR(L"创建梁"), GBMP_TR(L"此视图下不支持创建梁"), (int)UiCommonDialog::ButtonType::OK);
        return false;
    }
    return true;
}
